# GET Command Generator
# http://code.google.com/p/pysnmpcollector/
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto import rfc1902
import sqlite3


def setsnmp(ndip,oid,type,value):

	if type=="OctetString":
		errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.OctetString(value)))
	if type=="IpAddress":
		errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.IpAddress(value)))
	if type=="Integer":
		errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().setCmd(cmdgen.CommunityData(ndip, 'private'),cmdgen.UdpTransportTarget((ndip, 161)),(oid, rfc1902.Integer(value)))
		
	if errorIndication:
		print errorIndication
	else:
		if errorStatus:
			print '%s at %s\n' % (
				errorStatus.prettyPrint(),
				errorIndex and varBinds[int(errorIndex)-1] or '?'
				)
		else:
			for name, val in varBinds:
				print '%s = %s' % (name.prettyPrint(), val.prettyPrint())
							
def getsnmp(ndip,oid):
	errorIndication, errorStatus, errorIndex, varBinds = cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData(ndip, 'public'),cmdgen.UdpTransportTarget((ndip, 161)),oid,)

	if errorIndication:
		print errorIndication
		# conn.execute("update rnd set snmp"+str(oid[-1])+"='"+errorIndication+"' where ndip='"+ndip+"'")
	else:
		if errorStatus:
			print '%s at %s\n' % (
				errorStatus.prettyPrint(),
				errorIndex and varBinds[int(errorIndex)-1] or '?'
				)
			# conn.execute("update rnd set snmp"+str(oid[-1])+"='"+rrorStatus.prettyPrint()+"' where ndip='"+ndip+"'")
		else:
				# print varBinds
			for name, val in varBinds:
				# print ndip
				print '%s:%s = %s' % (ndip,name.prettyPrint(), val.prettyPrint())
				# print '-'.join('%02x' % ord(c) for c in  val)
				# print oid[-1]
				# conn.execute("update rnd set snmp"+str(oid[-1])+"='"+val.prettyPrint()+"' where ndip='"+ndip+"'")
	# conn.commit()
conn=sqlite3.connect(r'lkt.db3')
conn.text_factory = str
nds=conn.execute("select * from rnd where ssnmp0<>'172.16.100.31' or ssnmp1<>'172.16.100.40'").fetchall()
# conn.close()
for x in nds:
	# trap-server0							
	setsnmp(x[0],(1,3,6,1,4,1,5959,1,1,11,1,1,3,1,0),"IpAddress","172.16.100.31")		
	# trap-server1					
	setsnmp(x[0],(1,3,6,1,4,1,5959,1,1,11,1,1,3,1,1),"IpAddress","172.16.100.40")			
	# save config				
	setsnmp(x[0],(1,3,6,1,4,1,5959,1,1,4,1,1,11,1),"Integer","1")				
	# reboot			
	# setsnmp(x[0],(1,3,6,1,4,1,5959,1,1,4,1,1,10,1),"Integer","1")							
	
					